<style>
    .layui-form-label{
        width: 90px;
    }
    .layui-input-block {
        margin-left: 120px;
    }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">询价单号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="inquiryNo" class="layui-input" type="text" placeholder="输入询价单号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">请购单号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="receiptsNo" class="layui-input" type="text" placeholder="输入所属请购单号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">物料名称：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productName" class="layui-input" type="text" placeholder="输入物料名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">询价状态：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="enquiryStatus">
                                            <option value="">请选择询价状态</option>
                                            <option value="inquiry">询价中</option>
                                            <option value="finish">询价结束</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">是否公开：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="hasPublic">
                                            <option value="">是否公开</option>
                                            <option value="0">否</option>
                                            <option value="1">是</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 表格 -->
            <table class="layui-table" id="erpPurchaseQuotationEnquiry-table" lay-filter="erpPurchaseQuotationEnquiry-table"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="erpPurchaseQuotationEnquiry-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">详情</a>
    {{# if(d.hasGenerateOrder){ }}
    <a class="layui-btn layui-btn-xs" lay-event="generate">生成采购订单</a>
    {{# } }}
    <a class="layui-btn layui-btn-xs" lay-event="quotation">报价单</a>
</script>

<!-- erpPurchaseOrder表单弹窗 -->
<script type="text/html" id="erpPurchaseOrder-form">
    <form lay-filter="erpPurchaseOrder-form" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item">
                    <div class="layui-col-xs4">
                        <div class="layui-form-item">
                            <label class="layui-form-label">供应商<span style="color:red;">*</span></label>
                            <div class="layui-input-block">
                                <select name="supplierId" lay-filter="getSupplier" lay-verify="required" required lay-search></select>
                            </div>
                        </div>
                    </div>
                    <!--<div class="layui-col-xs4">
                        <div class="layui-form-item">
                            <label class="layui-form-label">是否过磅<span style="color:red;">*</span></label>
                            <div class="layui-input-block">
                                <select name="isWeigh" disabled>
                                    <option value="">请选择</option>
                                    <option value="1">是</option>
                                    <option value="2">否</option>
                                </select>
                            </div>
                        </div>
                    </div>-->
                    <div class="layui-col-xs4">
                        <div class="layui-form-item">
                            <label class="layui-form-label">部门</label>
                            <div class="layui-input-block">
                                <select name="departmentId" lay-search></select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs4">
                        <div class="layui-form-item">
                            <label class="layui-form-label">业务员</label>
                            <div class="layui-input-block">
                                <select name="personnelId" lay-search></select>
                            </div>
                        </div>
                    </div>
                </div>

                <table class="layui-table" id="erpPurchaseOrder-add-table" lay-filter="erpPurchaseOrder-add-table"></table>

            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
                <button class="layui-btn" lay-filter="erpPurchaseOrder-form-submit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="erpPurchaseOrderSupplierId">
    <option value="">请选择供应商</option>
    {{# layui.each(d, function(index, item){ }}
    <option value="{{item.id}}">{{item.corporateName}}</option>
    {{# }) }}
</script>

<script type="text/html" id="erpPurchaseOrderErpDepartmentId">
    <option value="">请选择部门</option>
    {{# layui.each(d, function(index, item){ }}
    <option value="{{item.id}}">{{item.code}}-{{item.name}}</option>
    {{# }) }}
</script>

<script type="text/html" id="erpPurchaseOrderErpPersonnelId">
    <option value="">请选择业务员</option>
    {{# layui.each(d, function(index, item){ }}
    <option value="{{item.id}}">{{item.nickname}}-{{item.username}}</option>
    {{# }) }}
</script>

<script type="text/html" id="erpInventoryList">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline mr0">
                            <input id="erpInventoryList-code" class="layui-input search-input" type="text" placeholder="输入物料编号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline mr0">
                            <input id="erpInventoryList-name" class="layui-input search-input" type="text" placeholder="输入物料名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline mr0">
                            <select id="erpInventoryList-way">
                                <option value="">请选择计价方式</option>
                                <option value="piece">按件</option>
                                <option value="weight">按重量</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input name="addSelfCollectedGoodsProductCategoryId" type="hidden" lay-verify="required">
                            <div class="layui-input-inline mr0" id="addSelfCollectedGoodsProductCategoryId"></div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="erpInventoryList-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="erpInventoryList-btn-choose" class="layui-btn layui-btn-normal">确认选择</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="erpInventoryList-table" lay-filter="erpInventoryList-table"></table>
        </div>
    </div>
</script>

<script type="text/html" id="erpInventoryListErpInventoryClassId">
    <option value="">请选择物料分类</option>
    {{# layui.each(d, function(index, item){ }}
    {{#
    var space = "";
    if(item.grade > 0){
    for(var j = 0; j < item.grade; j++){
    space += "&nbsp;&nbsp;";
    }
    }
    var disabled = "";
    if(item.isDisable == 1 || item.isParent){
    disabled = "disabled";
    }
    }}
    <option value="{{item.id}}" {{disabled}}>{{space}}{{item.code}}-{{item.name}}</option>
    {{# }) }}
</script>

<script type="text/html" id="quantity">
    {{# var quantity = "";
    var isRequired = false;
    if(d.quantity != null){
    quantity = d.quantity;
    isRequired = true;
    }
    }}
    <div><input name="quantity" placeholder="请输入数量" type="text" value="{{quantity}}" lay-verify="{{isRequired ? 'required|quantity' : ''}}" required="{{isRequired ? true : false}}" class="layui-input" maxlength="10" style="border: 0;height: 28px;text-align:center;" /></div>
</script>

<script type="text/html" id="rate">
    {{# var rate = "";
    var isRequired = false;
    if(d.rate){
    rate = d.rate;
    isRequired = true;
    }
    }}
    <div><input name="rate" placeholder="请输入税率" type="text" value="{{rate}}" lay-verify="{{isRequired ? 'required|rate' : ''}}" required="{{isRequired ? true : false}}" class="layui-input" maxlength="10" style="border: 0;height: 28px;text-align:center;" /></div>
</script>

<script type="text/html" id="taxPrice">
    {{# var taxPrice = "";
    var isRequired = false;
    if(d.taxPrice != null){
    taxPrice = d.taxPrice;
    isRequired = true;
    }
    }}
    <div><input name="taxPrice" placeholder="请输入含税单价" type="text" value="{{taxPrice}}" lay-verify="{{isRequired ? 'required|rate' : ''}}" required="{{isRequired ? true : false}}" class="layui-input" maxlength="10" style="border: 0;height: 28px;text-align:center;" /></div>
</script>

<script type="text/html" id="offerTaxPrice">
    {{# var offerTaxPrice = "";
    if(d.offerTaxPrice != null){
    offerTaxPrice = d.offerTaxPrice;
    }
    }}
    <div><input name="offerTaxPrice" placeholder="请输入报价含税单价" type="text" value="{{offerTaxPrice}}" lay-verify="quantity" class="layui-input" maxlength="10" style="border: 0;height: 28px;text-align:center;" /></div>
</script>

<script type="text/html" id="supply">
    {{# var supply = "";
    if(d.supply != null){
    supply = d.supply;
    }
    }}
    <div><input name="supply" placeholder="请输入可供应量" type="text" value="{{supply}}" lay-verify="quantity" class="layui-input" maxlength="10" style="border: 0;height: 28px;text-align:center;" /></div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="erpPurchaseOrder-add-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<!-- erpPurchaseQuotation表单弹窗 -->
<script type="text/html" id="erpPurchaseQuotationEnquiry-view">
    <form id="erpPurchaseQuotation-edit" lay-filter="erpPurchaseQuotation-edit" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto" style="padding-top: 5px;">
                <table class="layui-table" id="erpPurchaseQuotation-add-table" lay-filter="erpPurchaseQuotation-add-table"></table>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="erpPurchaseQuotation-add-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">详情</a>
</script>



<script type="text/html" id="quotationForm">
    <form lay-filter="quotationForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item">
                    <label class="layui-form-label">供应商<span style="color:red;">*</span></label>
                    <div class="layui-input-block">
                        <select name="supplierId" lay-filter="getSupplier" lay-verify="required" required></select>
                    </div>
                </div>

                <div id="quotationList">
                    <table class="layui-table" id="quotation-add-table" lay-filter="quotation-add-table"></table>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
                <button class="layui-btn" lay-filter="quotationFormSubmit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>


<script type="text/html" id="erpPurchaseQuotationEnquiry-quotationList">
    <div class="open-form">
        <div class="open-model">
            <div class="open-auto">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline mr0">
                                <input name="quotationNo" class="layui-input" type="text" placeholder="输入报价单号"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline mr0">
                                <input name="productName" class="layui-input" type="text" placeholder="输入物料名称"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline mr0">
                                <select name="supplierId" lay-search></select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="quotationFormSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                            <button id="erpPurchaseQuotationEnquiryBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            <!--<button id="erpPurchaseQuotationEnquiryBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="erpPurchaseQuotation-table" lay-filter="erpPurchaseQuotation-table"></table>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            </div>
        </div>
    </div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="erpPurchaseQuotation-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">详情</a>
    {{# if(d.status == 'confirm'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="confirm">确认</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reject">驳回</a>
    {{# } }}
    {{# if(d.status == 'finish'){ }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="cancel">取消确认</a>
    {{# } }}
</script>

<script type="text/html" id="quotation-detail">
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-table" id="quotation-detail-table" lay-filter="quotation-detail-table"></table>
        </div>
    </div>
</script>

<script type="text/html" id="quotation-detail-table-bar">
    <a class="layui-btn layui-btn-xs" lay-event="assign">分配采购量</a>
    <a class="layui-btn layui-btn-xs" lay-event="contrast">对比</a>
</script>

<script type="text/html" id="contrastQuotation-detail">
    <div class="open-form">
        <div class="open-model">
            <div class="open-auto" style="padding-top: 5px;">
                <table class="layui-table" id="contrastQuotation-detail-table" lay-filter="contrastQuotation-detail-table"></table>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="contrastQuotation-detail-table-bar">
    <a class="layui-btn layui-btn-xs" lay-event="assign">分配采购量</a>
    <a class="layui-btn layui-btn-xs" lay-event="view">详情</a>
</script>

<script type="text/html" id="assign-model">
    <form id="assign-form" lay-filter="assign-form" class="layui-form" style="margin-top: 20px;padding-right: 20px;">
        <div class="layui-form-item">
            <label class="layui-form-label">采购量</label>
            <div class="layui-input-block">
                <input name="requisitionsQuantity" type="text" disabled class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">已分配量</label>
            <div class="layui-input-block">
                <input name="allocation" type="text" disabled class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">剩余可分配量</label>
            <div class="layui-input-block">
                <input name="demandQuantity" type="text" disabled class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可供应量</label>
            <div class="layui-input-block">
                <input name="supply" type="text" disabled class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-block">
                <input name="unitName" type="text" disabled class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分配采购量<span style="color:red;">*</span></label>
            <div class="layui-input-block">
                <input name="allocationPurchases" placeholder="请输入分配采购量" type="text" maxlength="50" class="layui-input"/>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="viewModelDetail">
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-table" id="viewModelDetail-table" lay-filter="viewModelDetail-table"></table>
        </div>
    </div>
</script>

<script type="text/html" id="viewModelDetail-table-bar">
    {{# if(d.hasQuotation){ }}
    <a class="layui-btn layui-btn-xs" lay-event="assign">分配采购量</a>
    <a class="layui-btn layui-btn-xs" lay-event="contrast">对比</a>
    {{# } }}
</script>

<script type="text/html" id="erpPurchaseQuotationSupplierId_list">
    <option value="">请选择供应商</option>
    {{# layui.each(d, function(index, item){ }}
    <option value="{{item.id}}">{{item.corporateName}}</option>
    {{# }) }}
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin','laytpl','laydate','util','formSelects'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var util = layui.util;
        var formSelects = layui.formSelects;

        form.render("select");

        //开始日期
        var start_date = laydate.render({
            elem: '#startTime'
            ,format:'yyyy-MM-dd'
            ,done:function(value,date){
                end_date.config.min = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                }
            }
        });
        //结束日期
        var end_date = laydate.render({
            elem: '#endTime'
            ,format:'yyyy-MM-dd'
            ,done:function(value,date){
                if(value != ""){
                    start_date.config.max = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date,
                        hours: date.hours,
                        minutes: date.minutes,
                        seconds: date.seconds
                    }
                }else{
                    var curDate = new Date();
                    start_date.config.max = {
                        year: curDate.getFullYear()+100,
                        month: 12,
                        date: 31,
                        hours: 23,
                        minutes: 59,
                        seconds: 59
                    }
                }
            }
        });

        laydate.render({
            elem: '#deliveryStartTime'
        });
        laydate.render({
            elem: '#deliveryTime',
            type: 'datetime',
        });

        // 渲染表格
        var insTb = table.render({
            elem: '#erpPurchaseQuotationEnquiry-table',
            url: config.base + 'enterprise/purchase/admin/purchaseEnquiry/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', fixed: 'left'},
                {field: 'inquiryNo', align: 'center', title: '询价单号', width: 150},
                {field: 'receiptsNo', align: 'center', title: '所属请购单号', width: 150},
                {field: 'productNumber', align: 'center', title: '物料数量', width: 100},
                {field: 'productNames', align: 'center', title: '物料名称', width: 140},
                {align: 'center', title: '是否公开',templet:function(d){
                        return d.hasPublic ? "是":"否";
                    }, width: 100},
                {field: 'enquiryStatusDesc', align: 'center', title: '询价状态', width: 100},
                {field: 'inquiryEndTime', align: 'center', title: '询价结束时间', width: 170},
                {field: 'deliveryStartTime', align: 'center', title: '交货开始时间', width: 170},
                {field: 'deliveryTime', align: 'center', title: '交货截止时间', width: 170},
                {field: 'contactPerson', align: 'center', title: '联系人', width: 120},
                {field: 'contactNumber', align: 'center', title: '联系人电话', width: 150},
                {field: 'createTime', align: 'center', title: '创建时间', width: 170},
                {align: 'center', toolbar: '#erpPurchaseQuotationEnquiry-table-bar', title: '操作',width: 250, fixed: 'right'}
            ]]
        });

        // 工具条点击事件
        table.on('tool(erpPurchaseQuotationEnquiry-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (obj.event === 'generate') { //生成采购订单
                generate(data);
            } else if (obj.event === 'quotation') { //报价单
                quotation(data);
            } else if (obj.event === 'view') { //详情
                showViewModel(data);
            }
        });

        // 生成采购订单
        var generate = function (data) {
            var enquiryId = data.id;
            admin.open({
                type: 1,
                area: ['1200px','80%'],
                title: '新增采购订单信息',
                content: $("#erpPurchaseOrder-form").html(),
                success: function () {
                    var dataList = [];
                    var purchaseOrderDetails = []; //明细数据
                    var usualData = [{},{},{},{},{},{},{},{},{},{}];
                    var codeArr = [];//物料选择集合
                    var quotationDetailIds = []; //报价单明细ID集合

                    //自定义验证规则
                    form.verify({
                        quantity: [/^([1-9]\d*(\.\d{1,2})?|([0](\.([0][0-9][1-9]|[1-9]\d{0,1})))|[ \t]*)$/, '格式错误'],
                        rate: [/^([1-9]\d*(\.\d{1,2})?|([0](\.([0][1-9]|[1-9]\d{0,1})))|[ \t]*)$/, '格式错误']
                    })

                    // 获取供应商 -- 查询当前询价单报价的供应商
                    admin.req('enterprise/purchase/admin/supplierInfoPass/list', {limit: 99999, filterGenerateOrder: 1, enquiryId: enquiryId}, function (data) {
                        if (200 == data.code) {
                            laytpl(erpPurchaseOrderSupplierId.innerHTML).render(data.data, function (html) {
                                $("select[name='supplierId']").html(html);
                                form.render('select');
                            });

                            form.on('select(getSupplier)', function (data) {
                                if(data.value != ""){
                                    detail(data.value);
                                }else{
                                    detail();
                                }
                            })
                        } else {
                            layer.msg('获取供应商失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    // 获取部门
                    admin.req('user/admin/department/list', {limit:9999}, function (data) {
                        if (200 == data.code) {
                            laytpl(erpPurchaseOrderErpDepartmentId.innerHTML).render(data.data, function (html) {
                                $("select[name='departmentId']").html(html);
                                form.render('select');
                            });
                        } else {
                            layer.msg('获取部门失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    // 获取业务员
                    admin.req('user/admin/user/list', {limit:9999}, function (data) {
                        if (200 == data.code) {
                            laytpl(erpPurchaseOrderErpPersonnelId.innerHTML).render(data.data, function (html) {
                                $("select[name='personnelId']").html(html);
                                form.render('select');
                            });
                        } else {
                            layer.msg('获取业务员失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    function detail(supplierId){
                        dataList = [];
                        purchaseOrderDetails = []; //明细数据
                        usualData = [{},{},{},{},{},{},{},{},{},{}];
                        codeArr = [];//物料选择集合
                        quotationDetailIds = []; //报价单明细ID集合

                        if(supplierId){
                            //获取报价单明细数据
                            admin.req('enterprise/purchase/admin/purchaseQuotationDetail/list', {purchaseEnquiryId: data.id,
                                supplierId: supplierId, purchaseRequisitionsId: data.purchaseRequisitionsId,
                                hasGenerateOrder: 0}, function (data) {
                                if (200 == data.code) {
                                    var detail = data.data;
                                    console.log(detail);
                                    if(detail.length > 0){
                                        var isWeigh = detail[0].isWeigh;
                                        $("select[name='isWeigh']").val(isWeigh);
                                        form.render("select");
                                        for (var i = 0; i < detail.length; i++) {
                                            quotationDetailIds.push(detail[i].id);
                                            dataList.push({});
                                            dataList[i].skuCode = detail[i].skuCode;
                                            dataList[i].productName = detail[i].productName;
                                            dataList[i].unitName = detail[i].unitName;
                                            dataList[i].specifications = detail[i].specifications ? detail[i].specifications : "";
                                            dataList[i].quantity = detail[i].allocationPurchases ? detail[i].allocationPurchases : "";
                                            dataList[i].rate = detail[i].rate ? detail[i].rate : "";
                                            dataList[i].taxPrice = detail[i].offerTaxPrice ? detail[i].offerTaxPrice : "";
                                            dataList[i].price = detail[i].offerPrice ? detail[i].offerPrice : "";
                                            dataList[i].tax = detail[i].rateAmount ? detail[i].rateAmount : "";
                                            dataList[i].amount = detail[i].amount ? detail[i].amount : "";
                                            dataList[i].totalAmount = detail[i].totalAmount ? detail[i].totalAmount : "";
                                            purchaseOrderDetails.push({});
                                            purchaseOrderDetails[i].skuCode = detail[i].skuCode;
                                            purchaseOrderDetails[i].productName = detail[i].productName;
                                            purchaseOrderDetails[i].productDetailId = detail[i].productDetailId;
                                            purchaseOrderDetails[i].unitName = detail[i].unitName;
                                            purchaseOrderDetails[i].specifications = detail[i].specifications ? detail[i].specifications : "";
                                            purchaseOrderDetails[i].quantity = detail[i].allocationPurchases ? detail[i].allocationPurchases : "";
                                            purchaseOrderDetails[i].rate = detail[i].rate ? detail[i].rate : "";
                                            purchaseOrderDetails[i].taxPrice = detail[i].offerTaxPrice ? detail[i].offerTaxPrice : "";
                                            purchaseOrderDetails[i].price = detail[i].offerPrice ? detail[i].offerPrice : "";
                                            purchaseOrderDetails[i].tax = detail[i].rateAmount ? detail[i].rateAmount : "";
                                            purchaseOrderDetails[i].amount = detail[i].amount ? detail[i].amount : "";
                                            purchaseOrderDetails[i].totalAmount = detail[i].totalAmount ? detail[i].totalAmount : "";
                                            usualData.push({});
                                            usualData[i].quantity = detail[i].supply ? detail[i].supply : "";
                                            usualData[i].rate = detail[i].rate ? detail[i].rate : "";
                                            usualData[i].taxPrice = detail[i].offerTaxPrice ? detail[i].offerTaxPrice : "";
                                            codeArr.push(detail[i].skuCode);
                                        }
                                        dataList.push({},{},{},{},{},{},{},{},{},{});
                                        usualData.push({},{},{},{},{},{},{},{},{},{});
                                    }
                                    // 渲染表格
                                    table.render({
                                        elem: '#erpPurchaseOrder-add-table',
                                        data: dataList.length > 10 ? dataList : [{},{},{},{},{},{},{},{},{},{}],
                                        limit: dataList.length > 10 ? dataList.length : 10,
                                        height: 430,
                                        cols: [[
                                            {type: 'numbers'},
                                            {field: 'productName',align:'center', title: '物料名称<span style="color:red;">*</span>', templet: '<div><span class="erpInventoryId" style="display: block;cursor: pointer;" data-add="{{d.productName != null ? false : true}}">{{d.productName != null ? d.productName : "请选择物料"}}</span></div>', width: '150'},
                                            {field: 'skuCode', align:'center', title: '物料编号<span style="color:red;">*</span>', width: '150'},
                                            {field: 'specifications',  align:'center', title: '规格型号', width: '150'},
                                            {field: 'unitName', align:'center', title: '采购单位<span style="color:red;">*</span>', width: '100'},
                                            {field: 'quantity', align:'center', title: '数量<span style="color:red;">*</span>', templet: '#quantity', width: '130'},
                                            {field: 'rate', align:'center', title: '税率(%)<span style="color:red;">*</span>', templet: '#rate', width: '120'},
                                            {field: 'taxPrice', align:'center', title: '含税单价<span style="color:red;">*</span>', templet: '#taxPrice', width: '140'},
                                            {field: 'price', align:'center', title: '单价<span style="color:red;">*</span>', width: '100'},
                                            {field: 'amount', align:'center', title: '金额<span style="color:red;">*</span>', width: '100'},
                                            {field: 'tax', align:'center', title: '税额<span style="color:red;">*</span>', width: '100'},
                                            {field: 'totalAmount', align:'center', title: '合计金额<span style="color:red;">*</span>', width: '110'},
                                            {align: 'center', toolbar: '#erpPurchaseOrder-add-table-bar', title: '操作',width: 110}
                                        ]],
                                        done: function () {
                                            $("input[name='quantity']").on("input",function(e){
                                                //获取input输入的值
                                                var curValue = e.delegateTarget.value;
                                                var curIndex = $(this).closest("tr").data("index");
                                                if(purchaseOrderDetails[curIndex]){
                                                    purchaseOrderDetails[curIndex].quantity = curValue;
                                                }
                                                dataList[curIndex].quantity = curValue;
                                                usualData[curIndex].quantity = curValue;
                                                if(usualData.length - curIndex - 1 < 10){
                                                    var addLength = 10 - (usualData.length - curIndex - 1);
                                                    for (var i = 0; i < addLength; i++) {
                                                        usualData.push({});
                                                    }
                                                }
                                                var rate = "";
                                                var taxPrice = "";
                                                $(this).closest("tr").find("td").each(function () {
                                                    if($(this).data("field") == "rate"){
                                                        rate = $(this).find("input").val();
                                                    }
                                                    if($(this).data("field") == "taxPrice"){
                                                        taxPrice = $(this).find("input").val();
                                                    }
                                                });
                                                //计算价格
                                                calculatePrice(curValue, rate, taxPrice, $(this), curIndex);
                                            });
                                            $("input[name='rate']").on("input",function(e){
                                                //获取input输入的值
                                                var curValue = e.delegateTarget.value;
                                                var curIndex = $(this).closest("tr").data("index");
                                                if(purchaseOrderDetails[curIndex]){
                                                    purchaseOrderDetails[curIndex].rate = curValue;
                                                }
                                                dataList[curIndex].rate = curValue;
                                                usualData[curIndex].rate = curValue;
                                                if(usualData.length - curIndex - 1 < 10){
                                                    var addLength = 10 - (usualData.length - curIndex - 1);
                                                    for (var i = 0; i < addLength; i++) {
                                                        usualData.push({});
                                                    }
                                                }
                                                var quantity = "";
                                                var taxPrice = "";
                                                $(this).closest("tr").find("td").each(function () {
                                                    if($(this).data("field") == "quantity"){
                                                        quantity = $(this).find("input").val();
                                                    }
                                                    if($(this).data("field") == "taxPrice"){
                                                        taxPrice = $(this).find("input").val();
                                                    }
                                                });
                                                //计算价格
                                                calculatePrice(quantity, curValue, taxPrice, $(this), curIndex);
                                            });
                                            $("input[name='taxPrice']").on("input",function(e){
                                                //获取input输入的值
                                                var curValue = e.delegateTarget.value;
                                                var curIndex = $(this).closest("tr").data("index");
                                                if(purchaseOrderDetails[curIndex]){
                                                    purchaseOrderDetails[curIndex].taxPrice = curValue;
                                                }
                                                dataList[curIndex].taxPrice = curValue;
                                                usualData[curIndex].taxPrice = curValue;
                                                if(usualData.length - curIndex - 1 < 10){
                                                    var addLength = 10 - (usualData.length - curIndex - 1);
                                                    for (var i = 0; i < addLength; i++) {
                                                        usualData.push({});
                                                    }
                                                }
                                                var quantity = "";
                                                var rate = "";
                                                $(this).closest("tr").find("td").each(function () {
                                                    if($(this).data("field") == "quantity"){
                                                        quantity = $(this).find("input").val();
                                                    }
                                                    if($(this).data("field") == "rate"){
                                                        rate = $(this).find("input").val();
                                                    }
                                                });
                                                //计算价格
                                                calculatePrice(quantity, rate, curValue, $(this), curIndex);
                                            });
                                            //通过数量、税率、含税单价计算价格
                                            function calculatePrice(quantity, rate, taxPrice, $cur, curIndex){
                                                //计算单价
                                                if(rate != "" && taxPrice != ""){
                                                    var price = taxPrice*100/(Number(rate)+100);
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "price"){
                                                            $(this).find("div").text(price.toFixed(2));
                                                            dataList[curIndex].price = price.toFixed(2);
                                                            usualData[curIndex].price = price.toFixed(2);
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].price = price.toFixed(2);
                                                            }
                                                        }
                                                    });
                                                }else{
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "price"){
                                                            $(this).find("div").text("");
                                                            dataList[curIndex].price = "";
                                                            usualData[curIndex].price = "";
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].price = "";
                                                            }
                                                        }
                                                    });
                                                }
                                                //计算金额
                                                if(quantity != "" && rate != "" && taxPrice != ""){
                                                    var amount = quantity*taxPrice*100/(Number(rate)+100);
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "amount"){
                                                            $(this).find("div").text(amount.toFixed(2));
                                                            dataList[curIndex].amount = amount.toFixed(2);
                                                            usualData[curIndex].amount = amount.toFixed(2);
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].amount = amount.toFixed(2);
                                                            }
                                                        }
                                                    });
                                                }else{
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "amount"){
                                                            $(this).find("div").text("");
                                                            dataList[curIndex].amount = "";
                                                            usualData[curIndex].amount = "";
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].amount = "";
                                                            }
                                                        }
                                                    });
                                                }
                                                //计算税额
                                                if(quantity != "" && rate != "" && taxPrice != ""){
                                                    var tax = quantity*taxPrice - quantity*taxPrice*100/(Number(rate)+100);
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "tax"){
                                                            $(this).find("div").text(tax.toFixed(2));
                                                            dataList[curIndex].tax = tax.toFixed(2);
                                                            usualData[curIndex].tax = tax.toFixed(2);
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].tax = tax.toFixed(2);
                                                            }
                                                        }
                                                    });
                                                }else{
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "tax"){
                                                            $(this).find("div").text("");
                                                            dataList[curIndex].tax = "";
                                                            usualData[curIndex].tax = "";
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].tax = "";
                                                            }
                                                        }
                                                    });
                                                }
                                                //计算合计金额
                                                if(quantity != "" && taxPrice != ""){
                                                    var totalAmount = quantity*taxPrice;
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "totalAmount"){
                                                            $(this).find("div").text(totalAmount.toFixed(2));
                                                            dataList[curIndex].totalAmount = totalAmount.toFixed(2);
                                                            usualData[curIndex].totalAmount = totalAmount.toFixed(2);
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].totalAmount = totalAmount.toFixed(2);
                                                            }
                                                        }
                                                    });
                                                }else{
                                                    $cur.closest("tr").find("td").each(function () {
                                                        if($(this).data("field") == "totalAmount"){
                                                            $(this).find("div").text("");
                                                            dataList[curIndex].totalAmount = "";
                                                            usualData[curIndex].totalAmount = "";
                                                            if(purchaseOrderDetails[curIndex]){
                                                                purchaseOrderDetails[curIndex].totalAmount = "";
                                                            }
                                                        }
                                                    });
                                                }
                                            }
                                            layer.closeAll('loading');
                                            $(".erpInventoryId").each(function (index) {
                                                $(this).click(function () {
                                                    var isWeigh = $("select[name='isWeigh']").val();
                                                    if(isWeigh != "") {
                                                        var isAdd = $(this).data("add");
                                                        //获取物料数据
                                                        var erpInventoryList = layer.open({
                                                            type: 1,
                                                            title: '物料列表',
                                                            area: ['1100px', '700px'],
                                                            offset: '120px',
                                                            content: $('#erpInventoryList').html(),
                                                            success: function () {
                                                                form.render("select");

                                                                // 请求物料分类
                                                                let arr2tree;
                                                                admin.reqSync('enterprise/product/admin/productCategory/getProductCategoryByType', {type: 0}, function (res) {
                                                                    for (let i = 0; i < res.data.length; i++) {
                                                                        res.data[i].children = [];
                                                                    }
                                                                    arr2tree = xmSelect.arr2tree(res.data, "parentId", "id", "children", -1);
                                                                }, 'get', false);

                                                                let smlSelectInstans = xmSelect.render({
                                                                    el: '#addSelfCollectedGoodsProductCategoryId',
                                                                    model: {label: {type: 'text'}},
                                                                    radio: true,
                                                                    clickClose: true,
                                                                    filterable: true,
                                                                    tree: {
                                                                        show: true,
                                                                        strict: false,
                                                                        expandedKeys: [-1],
                                                                    },
                                                                    tips: '请选择物料分类',
                                                                    prop: {
                                                                        name: 'name',
                                                                        value: 'id',
                                                                        children: 'children'
                                                                    },
                                                                    data() {
                                                                        //填充数据
                                                                        return arr2tree;
                                                                    },
                                                                    on(data) {
                                                                        let parentId = data.arr.length > 0 ? data.change[0].id : "";
                                                                        $("input[name='addSelfCollectedGoodsProductCategoryId']").val(parentId);
                                                                    }
                                                                });

                                                                var codes = "";
                                                                for (var i = 0; i < codeArr.length; i++) {
                                                                    if(i == 0){
                                                                        codes = '"'+codeArr[i]+'"';
                                                                    }else{
                                                                        codes += "," + '"'+codeArr[i]+'"';
                                                                    }
                                                                }

                                                                // 渲染表格
                                                                table.render({
                                                                    elem: '#erpInventoryList-table',
                                                                    url: config.base + 'enterprise/product/admin/productDetail/list',
                                                                    method: 'GET',
                                                                    where: {
                                                                        codes: codes
                                                                    },
                                                                    page: true,
                                                                    cols: [[
                                                                        {type: 'checkbox'},
                                                                         {field: 'skuCode', align: 'center', title: '物料编号'},
                                                                        {field: 'skuName', align: 'center', title: '物料名称'},
                                                                        {field: 'skuSpecValueNames', align: 'center', title: '规格型号'},
                                                                        {field: 'pricingMethodDesc', align: 'center', title: '计价方式'},
                                                                        {field: 'categoryName', align: 'center', title: '物料分类'},
                                                                        {field: 'unitName', align: 'center', title: '计量单位'}
                                                                    ]]
                                                                });

                                                                // 搜索按钮点击事件
                                                                $('#erpInventoryList-btn-search').click(function () {
                                                                    var name = $('#erpInventoryList-name').val();
                                                                    var code = $('#erpInventoryList-code').val();
                                                                    var way = $('#erpInventoryList-way').val();
                                                                    var categoryId = $('input[name=addSelfCollectedGoodsProductCategoryId]').val();
                                                                    table.reload('erpInventoryList-table', {page: {curr:1}, where: {skuCode: code, skuName: name, pricingMethod: way, categoryId: categoryId}});
                                                                });

                                                                // 确认选择
                                                                $('#erpInventoryList-btn-choose').click(function () {
                                                                    var checkStatus = table.checkStatus('erpInventoryList-table'); //test即为基础参数id对应的值
                                                                    var dataS = checkStatus.data;
                                                                    for (var i = 0; i < dataS.length; i++) {
                                                                        dataS[i].productName = dataS[i].skuName;
                                                                        dataS[i].specifications = dataS[i].skuSpecValueNames;
                                                                    }
                                                                    if (checkStatus.data.length == 0) {
                                                                        layer.msg("请选择物料", {icon: 2, time: 1000});
                                                                    }else{
                                                                        layer.close(erpInventoryList);
                                                                        if(dataList.length >= 10){
                                                                            dataList.splice(dataList.length-10, 10);
                                                                        }
                                                                        if(!isAdd){
                                                                            dataList.splice(index, 1);
                                                                            usualData.splice(index, 1);
                                                                            codeArr.splice(index, 1);
                                                                            for (var i = 0; i < dataS.length; i++) {
                                                                                dataList.splice(index+i, 0, dataS[i]);
                                                                                usualData.splice(index+i, 0, {})
                                                                                codeArr.splice(index+i, 0, dataS[i].skuCode);
                                                                            }
                                                                        }else{
                                                                            for (var i = 0; i < dataS.length; i++) {
                                                                                dataList.push(dataS[i]);
                                                                                usualData.push({})
                                                                                codeArr.push(dataS[i].skuCode);
                                                                            }
                                                                        }
                                                                        //处理数据并存储
                                                                        for (var i = 0; i < dataList.length; i++) {
                                                                            if(!purchaseOrderDetails[i]){
                                                                                purchaseOrderDetails.push({});
                                                                            }
                                                                            purchaseOrderDetails[i].skuCode = dataList[i].skuCode;
                                                                            purchaseOrderDetails[i].productName = dataList[i].skuName;
                                                                            purchaseOrderDetails[i].unitName = dataList[i].unitName;
                                                                            purchaseOrderDetails[i].specifications = dataList[i].skuSpecValueNames;
                                                                            if(usualData[i]){
                                                                                dataList[i].quantity = usualData[i].quantity ? usualData[i].quantity : "";
                                                                                dataList[i].rate = usualData[i].rate ? usualData[i].rate : "";
                                                                                dataList[i].taxPrice = usualData[i].taxPrice ? usualData[i].taxPrice : "";
                                                                                dataList[i].price = usualData[i].price ? usualData[i].price : "";
                                                                                dataList[i].amount = usualData[i].amount ? usualData[i].amount : "";
                                                                                dataList[i].tax = usualData[i].tax ? usualData[i].tax : "";
                                                                                dataList[i].totalAmount = usualData[i].totalAmount ? usualData[i].totalAmount : "";
                                                                                purchaseOrderDetails[i].quantity = usualData[i].quantity ? usualData[i].quantity : "";
                                                                                purchaseOrderDetails[i].rate = usualData[i].rate ? usualData[i].rate : "";
                                                                                purchaseOrderDetails[i].productDetailId = usualData[i].productDetailId ? usualData[i].productDetailId : "";
                                                                                purchaseOrderDetails[i].taxPrice = usualData[i].taxPrice ? usualData[i].taxPrice : "";
                                                                                purchaseOrderDetails[i].price = usualData[i].price ? usualData[i].price : "";
                                                                                purchaseOrderDetails[i].amount = usualData[i].amount ? usualData[i].amount : "";
                                                                                purchaseOrderDetails[i].tax = usualData[i].tax ? usualData[i].tax : "";
                                                                                purchaseOrderDetails[i].totalAmount = usualData[i].totalAmount ? usualData[i].totalAmount : "";
                                                                            }else{
                                                                                purchaseOrderDetails[i].quantity = "";
                                                                                purchaseOrderDetails[i].rate = "";
                                                                                purchaseOrderDetails[i].productDetailId = "";
                                                                                purchaseOrderDetails[i].taxPrice = "";
                                                                                purchaseOrderDetails[i].price = "";
                                                                                purchaseOrderDetails[i].amount = "";
                                                                                purchaseOrderDetails[i].tax = "";
                                                                                purchaseOrderDetails[i].totalAmount = "";

                                                                            }
                                                                        }
                                                                        dataList.push({},{},{},{},{},{},{},{},{},{});
                                                                        if(usualData.length - dataList.length > 0){
                                                                            usualData.splice(dataList.length+1, usualData.length - dataList.length);
                                                                        }else{
                                                                            var addLength = dataList.length - usualData.length;
                                                                            for (var i = 0; i < addLength; i++) {
                                                                                usualData.push({});
                                                                            }
                                                                        }
                                                                        // 获取数据集合并传入采购订单中
                                                                        table.reload('erpPurchaseOrder-add-table', {
                                                                            data: dataList,
                                                                            limit: dataList.length
                                                                        });
                                                                    }
                                                                });

                                                            }
                                                        })
                                                    }else{
                                                        layer.msg("请选择是否过磅", {icon: 2, time: 1000});
                                                    }
                                                })
                                            })
                                        }
                                    });
                                } else {
                                    layer.msg(data.msg, {icon: 2, time: 1000});
                                }
                            }, 'GET');
                        }else{
                            // 渲染表格
                            table.render({
                                elem: '#erpPurchaseOrder-add-table',
                                data: [{},{},{},{},{},{},{},{},{},{}],
                                limit: 10,
                                height: 430,
                                cols: [[
                                    {type: 'numbers'},
                                    {field: 'productName',align:'center', title: '物料名称<span style="color:red;">*</span>', templet: '<div><span class="erpInventoryId" style="display: block;cursor: pointer;" data-add="{{d.productName != null ? false : true}}">{{d.productName != null ? d.productName : "请选择物料"}}</span></div>', width: '150'},
                                    {field: 'skuCode', align:'center', title: '物料编号<span style="color:red;">*</span>', width: '150'},
                                    {field: 'specifications',  align:'center', title: '规格型号', width: '150'},
                                    {field: 'unitName', align:'center', title: '采购单位<span style="color:red;">*</span>', width: '100'},
                                    {field: 'quantity', align:'center', title: '数量<span style="color:red;">*</span>', templet: '#quantity', width: '130'},
                                    {field: 'rate', align:'center', title: '税率(%)<span style="color:red;">*</span>', templet: '#rate', width: '120'},
                                    {field: 'taxPrice', align:'center', title: '含税单价<span style="color:red;">*</span>', templet: '#taxPrice', width: '140'},
                                    {field: 'price', align:'center', title: '单价<span style="color:red;">*</span>', width: '100'},
                                    {field: 'amount', align:'center', title: '金额<span style="color:red;">*</span>', width: '100'},
                                    {field: 'tax', align:'center', title: '税额<span style="color:red;">*</span>', width: '100'},
                                    {field: 'totalAmount', align:'center', title: '合计金额<span style="color:red;">*</span>', width: '110'},
                                    {align: 'center', toolbar: '#erpPurchaseOrder-add-table-bar', title: '操作',width: 110}
                                ]],
                                done: function () {
                                    $("input[name='quantity']").on("input",function(e){
                                        //获取input输入的值
                                        var curValue = e.delegateTarget.value;
                                        var curIndex = $(this).closest("tr").data("index");
                                        if(purchaseOrderDetails[curIndex]){
                                            purchaseOrderDetails[curIndex].quantity = curValue;
                                        }
                                        dataList[curIndex].quantity = curValue;
                                        usualData[curIndex].quantity = curValue;
                                        if(usualData.length - curIndex - 1 < 10){
                                            var addLength = 10 - (usualData.length - curIndex - 1);
                                            for (var i = 0; i < addLength; i++) {
                                                usualData.push({});
                                            }
                                        }
                                        var rate = "";
                                        var taxPrice = "";
                                        $(this).closest("tr").find("td").each(function () {
                                            if($(this).data("field") == "rate"){
                                                rate = $(this).find("input").val();
                                            }
                                            if($(this).data("field") == "taxPrice"){
                                                taxPrice = $(this).find("input").val();
                                            }
                                        });
                                        //计算价格
                                        calculatePrice(curValue, rate, taxPrice, $(this), curIndex);
                                    });
                                    $("input[name='rate']").on("input",function(e){
                                        //获取input输入的值
                                        var curValue = e.delegateTarget.value;
                                        var curIndex = $(this).closest("tr").data("index");
                                        if(purchaseOrderDetails[curIndex]){
                                            purchaseOrderDetails[curIndex].rate = curValue;
                                        }
                                        dataList[curIndex].rate = curValue;
                                        usualData[curIndex].rate = curValue;
                                        if(usualData.length - curIndex - 1 < 10){
                                            var addLength = 10 - (usualData.length - curIndex - 1);
                                            for (var i = 0; i < addLength; i++) {
                                                usualData.push({});
                                            }
                                        }
                                        var quantity = "";
                                        var taxPrice = "";
                                        $(this).closest("tr").find("td").each(function () {
                                            if($(this).data("field") == "quantity"){
                                                quantity = $(this).find("input").val();
                                            }
                                            if($(this).data("field") == "taxPrice"){
                                                taxPrice = $(this).find("input").val();
                                            }
                                        });
                                        //计算价格
                                        calculatePrice(quantity, curValue, taxPrice, $(this), curIndex);
                                    });
                                    $("input[name='taxPrice']").on("input",function(e){
                                        //获取input输入的值
                                        var curValue = e.delegateTarget.value;
                                        var curIndex = $(this).closest("tr").data("index");
                                        if(purchaseOrderDetails[curIndex]){
                                            purchaseOrderDetails[curIndex].taxPrice = curValue;
                                        }
                                        dataList[curIndex].taxPrice = curValue;
                                        usualData[curIndex].taxPrice = curValue;
                                        if(usualData.length - curIndex - 1 < 10){
                                            var addLength = 10 - (usualData.length - curIndex - 1);
                                            for (var i = 0; i < addLength; i++) {
                                                usualData.push({});
                                            }
                                        }
                                        var quantity = "";
                                        var rate = "";
                                        $(this).closest("tr").find("td").each(function () {
                                            if($(this).data("field") == "quantity"){
                                                quantity = $(this).find("input").val();
                                            }
                                            if($(this).data("field") == "rate"){
                                                rate = $(this).find("input").val();
                                            }
                                        });
                                        //计算价格
                                        calculatePrice(quantity, rate, curValue, $(this), curIndex);
                                    });
                                    //通过数量、税率、含税单价计算价格
                                    function calculatePrice(quantity, rate, taxPrice, $cur, curIndex){
                                        //计算单价
                                        if(rate != "" && taxPrice != ""){
                                            var price = taxPrice*100/(Number(rate)+100);
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "price"){
                                                    $(this).find("div").text(price.toFixed(2));
                                                    dataList[curIndex].price = price.toFixed(2);
                                                    usualData[curIndex].price = price.toFixed(2);
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].price = price.toFixed(2);
                                                    }
                                                }
                                            });
                                        }else{
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "price"){
                                                    $(this).find("div").text("");
                                                    dataList[curIndex].price = "";
                                                    usualData[curIndex].price = "";
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].price = "";
                                                    }
                                                }
                                            });
                                        }
                                        //计算金额
                                        if(quantity != "" && rate != "" && taxPrice != ""){
                                            var amount = quantity*taxPrice*100/(Number(rate)+100);
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "amount"){
                                                    $(this).find("div").text(amount.toFixed(2));
                                                    dataList[curIndex].amount = amount.toFixed(2);
                                                    usualData[curIndex].amount = amount.toFixed(2);
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].amount = amount.toFixed(2);
                                                    }
                                                }
                                            });
                                        }else{
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "amount"){
                                                    $(this).find("div").text("");
                                                    dataList[curIndex].amount = "";
                                                    usualData[curIndex].amount = "";
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].amount = "";
                                                    }
                                                }
                                            });
                                        }
                                        //计算税额
                                        if(quantity != "" && rate != "" && taxPrice != ""){
                                            var tax = quantity*taxPrice - quantity*taxPrice*100/(Number(rate)+100);
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "tax"){
                                                    $(this).find("div").text(tax.toFixed(2));
                                                    dataList[curIndex].tax = tax.toFixed(2);
                                                    usualData[curIndex].tax = tax.toFixed(2);
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].tax = tax.toFixed(2);
                                                    }
                                                }
                                            });
                                        }else{
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "tax"){
                                                    $(this).find("div").text("");
                                                    dataList[curIndex].tax = "";
                                                    usualData[curIndex].tax = "";
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].tax = "";
                                                    }
                                                }
                                            });
                                        }
                                        //计算合计金额
                                        if(quantity != "" && taxPrice != ""){
                                            var totalAmount = quantity*taxPrice;
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "totalAmount"){
                                                    $(this).find("div").text(totalAmount.toFixed(2));
                                                    dataList[curIndex].totalAmount = totalAmount.toFixed(2);
                                                    usualData[curIndex].totalAmount = totalAmount.toFixed(2);
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].totalAmount = totalAmount.toFixed(2);
                                                    }
                                                }
                                            });
                                        }else{
                                            $cur.closest("tr").find("td").each(function () {
                                                if($(this).data("field") == "totalAmount"){
                                                    $(this).find("div").text("");
                                                    dataList[curIndex].totalAmount = "";
                                                    usualData[curIndex].totalAmount = "";
                                                    if(purchaseOrderDetails[curIndex]){
                                                        purchaseOrderDetails[curIndex].totalAmount = "";
                                                    }
                                                }
                                            });
                                        }
                                    }
                                    layer.closeAll('loading');
                                    $(".erpInventoryId").each(function (index) {
                                        $(this).click(function () {
                                            var isWeigh = $("select[name='isWeigh']").val();
                                            if(isWeigh != "") {
                                                var isAdd = $(this).data("add");
                                                //获取物料数据
                                                var erpInventoryList = layer.open({
                                                    type: 1,
                                                    title: '物料列表',
                                                    area: ['1100px', '700px'],
                                                    offset: '120px',
                                                    content: $('#erpInventoryList').html(),
                                                    success: function () {
                                                        form.render("select");

                                                        // 请求物料分类
                                                        let arr2tree;
                                                        admin.reqSync('enterprise/product/admin/productCategory/getProductCategoryByType', {type: 0}, function (res) {
                                                            for (let i = 0; i < res.data.length; i++) {
                                                                res.data[i].children = [];
                                                            }
                                                            arr2tree = xmSelect.arr2tree(res.data, "parentId", "id", "children", -1);
                                                        }, 'get', false);

                                                        let smlSelectInstans = xmSelect.render({
                                                            el: '#addSelfCollectedGoodsProductCategoryId',
                                                            model: {label: {type: 'text'}},
                                                            radio: true,
                                                            clickClose: true,
                                                            filterable: true,
                                                            tree: {
                                                                show: true,
                                                                strict: false,
                                                                expandedKeys: [-1],
                                                            },
                                                            tips: '请选择物料分类',
                                                            prop: {
                                                                name: 'name',
                                                                value: 'id',
                                                                children: 'children'
                                                            },
                                                            data() {
                                                                //填充数据
                                                                return arr2tree;
                                                            },
                                                            on(data) {
                                                                let parentId = data.arr.length > 0 ? data.change[0].id : "";
                                                                $("input[name='addSelfCollectedGoodsProductCategoryId']").val(parentId);
                                                            }
                                                        });

                                                        var codes = "";
                                                        for (var i = 0; i < codeArr.length; i++) {
                                                            if(i == 0){
                                                                codes = '"'+codeArr[i]+'"';
                                                            }else{
                                                                codes += "," + '"'+codeArr[i]+'"';
                                                            }
                                                        }

                                                        // 渲染表格
                                                        table.render({
                                                            elem: '#erpInventoryList-table',
                                                            url: config.base + 'enterprise/product/admin/productDetail/list',
                                                            method: 'GET',
                                                            where: {
                                                                codes: codes
                                                            },
                                                            page: true,
                                                            cols: [[
                                                                {type: 'checkbox'},
                                                                 {field: 'skuCode', align: 'center', title: '物料编号'},
                                                                {field: 'skuName', align: 'center', title: '物料名称'},
                                                                {field: 'skuSpecValueNames', align: 'center', title: '规格型号'},
                                                                {field: 'pricingMethodDesc', align: 'center', title: '计价方式'},
                                                                {field: 'categoryName', align: 'center', title: '物料分类'},
                                                                {field: 'unitName', align: 'center', title: '计量单位'}
                                                            ]]
                                                        });

                                                        // 搜索按钮点击事件
                                                        $('#erpInventoryList-btn-search').click(function () {
                                                            var name = $('#erpInventoryList-name').val();
                                                            var code = $('#erpInventoryList-code').val();
                                                            var way = $('#erpInventoryList-way').val();
                                                            var categoryId = $('input[name=addSelfCollectedGoodsProductCategoryId]').val();
                                                            table.reload('erpInventoryList-table', {page: {curr:1}, where: {skuCode: code, skuName: name, pricingMethod: way, categoryId: categoryId}});
                                                        });

                                                        // 确认选择
                                                        $('#erpInventoryList-btn-choose').click(function () {
                                                            var checkStatus = table.checkStatus('erpInventoryList-table'); //test即为基础参数id对应的值
                                                            var dataS = checkStatus.data;
                                                            for (var i = 0; i < dataS.length; i++) {
                                                                dataS[i].productName = dataS[i].skuName;
                                                                dataS[i].specifications = dataS[i].skuSpecValueNames;
                                                            }
                                                            if (checkStatus.data.length == 0) {
                                                                layer.msg("请选择物料", {icon: 2, time: 1000});
                                                            }else{
                                                                layer.close(erpInventoryList);
                                                                if(dataList.length >= 10){
                                                                    dataList.splice(dataList.length-10, 10);
                                                                }
                                                                if(!isAdd){
                                                                    dataList.splice(index, 1);
                                                                    usualData.splice(index, 1);
                                                                    codeArr.splice(index, 1);
                                                                    for (var i = 0; i < dataS.length; i++) {
                                                                        dataList.splice(index+i, 0, dataS[i]);
                                                                        usualData.splice(index+i, 0, {})
                                                                        codeArr.splice(index+i, 0, dataS[i].skuCode);
                                                                    }
                                                                }else{
                                                                    for (var i = 0; i < dataS.length; i++) {
                                                                        dataList.push(dataS[i]);
                                                                        usualData.push({})
                                                                        codeArr.push(dataS[i].skuCode);
                                                                    }
                                                                }
                                                                //处理数据并存储
                                                                for (var i = 0; i < dataList.length; i++) {
                                                                    if(!purchaseOrderDetails[i]){
                                                                        purchaseOrderDetails.push({});
                                                                    }
                                                                    purchaseOrderDetails[i].skuCode = dataList[i].skuCode;
                                                                    purchaseOrderDetails[i].productName = dataList[i].skuName;
                                                                    purchaseOrderDetails[i].unitName = dataList[i].unitName;
                                                                    purchaseOrderDetails[i].productDetailId = dataList[i].productDetailId;
                                                                    purchaseOrderDetails[i].specifications = dataList[i].skuSpecValueNames;
                                                                    if(usualData[i]){
                                                                        dataList[i].quantity = usualData[i].quantity ? usualData[i].quantity : "";
                                                                        dataList[i].rate = usualData[i].rate ? usualData[i].rate : "";
                                                                        dataList[i].taxPrice = usualData[i].taxPrice ? usualData[i].taxPrice : "";
                                                                        dataList[i].price = usualData[i].price ? usualData[i].price : "";
                                                                        dataList[i].amount = usualData[i].amount ? usualData[i].amount : "";
                                                                        dataList[i].tax = usualData[i].tax ? usualData[i].tax : "";
                                                                        dataList[i].totalAmount = usualData[i].totalAmount ? usualData[i].totalAmount : "";
                                                                        purchaseOrderDetails[i].quantity = usualData[i].quantity ? usualData[i].quantity : "";
                                                                        purchaseOrderDetails[i].rate = usualData[i].rate ? usualData[i].rate : "";
                                                                        purchaseOrderDetails[i].taxPrice = usualData[i].taxPrice ? usualData[i].taxPrice : "";
                                                                        purchaseOrderDetails[i].price = usualData[i].price ? usualData[i].price : "";
                                                                        purchaseOrderDetails[i].amount = usualData[i].amount ? usualData[i].amount : "";
                                                                        purchaseOrderDetails[i].productDetailId = usualData[i].productDetailId ? usualData[i].productDetailId : "";
                                                                        purchaseOrderDetails[i].tax = usualData[i].tax ? usualData[i].tax : "";
                                                                        purchaseOrderDetails[i].totalAmount = usualData[i].totalAmount ? usualData[i].totalAmount : "";
                                                                    }else{
                                                                        purchaseOrderDetails[i].quantity = "";
                                                                        purchaseOrderDetails[i].rate = "";
                                                                        purchaseOrderDetails[i].taxPrice = "";
                                                                        purchaseOrderDetails[i].price = "";
                                                                        purchaseOrderDetails[i].productDetailId = "";
                                                                        purchaseOrderDetails[i].amount = "";
                                                                        purchaseOrderDetails[i].tax = "";
                                                                        purchaseOrderDetails[i].totalAmount = "";

                                                                    }
                                                                }
                                                                dataList.push({},{},{},{},{},{},{},{},{},{});
                                                                if(usualData.length - dataList.length > 0){
                                                                    usualData.splice(dataList.length+1, usualData.length - dataList.length);
                                                                }else{
                                                                    var addLength = dataList.length - usualData.length;
                                                                    for (var i = 0; i < addLength; i++) {
                                                                        usualData.push({});
                                                                    }
                                                                }
                                                                // 获取数据集合并传入采购订单中
                                                                table.reload('erpPurchaseOrder-add-table', {
                                                                    data: dataList,
                                                                    limit: dataList.length
                                                                });
                                                            }
                                                        });

                                                    }
                                                })
                                            }else{
                                                layer.msg("请选择是否过磅", {icon: 2, time: 1000});
                                            }
                                        })
                                    })
                                }
                            });
                        }
                    }

                    // 工具条点击事件
                    table.on('tool(erpPurchaseOrder-add-table)', function (obj) {
                        var data = obj.data;
                        var layEvent = obj.event;
                        if (obj.event === 'del') { //删除
                            doDelete(obj);
                        }
                    });

                    // 删除
                    var doDelete = function (obj) {
                        var index = $(obj.tr).data("index");
                        if(!$.isEmptyObject(obj.data)){
                            layer.confirm('确定要删除吗？',{
                                offset: '65px',
                                skin: 'layui-layer-admin'
                            }, function (i) {
                                layer.close(i);
                                dataList.splice(index,1);
                                purchaseOrderDetails.splice(index,1);
                                usualData.splice(index,1);
                                quotationDetailIds.splice(index,1);
                                if(codeArr.indexOf(obj.data.skuCode) >= 0){
                                    codeArr.splice(codeArr.indexOf(obj.data.skuCode),1);
                                }
                                if($.isEmptyObject(dataList[0])){
                                    dataList = [];
                                    // 获取数据集合并传入请购单中
                                    table.reload('erpPurchaseOrder-add-table', {
                                        data: [{},{},{},{},{},{},{},{},{},{}],
                                        limit: 10,
                                    });
                                }else{
                                    // 获取数据集合并传入请购单中
                                    table.reload('erpPurchaseOrder-add-table', {
                                        data: dataList,
                                        limit: dataList.length
                                    });
                                }
                            });
                        }
                    };

                    // 表单提交事件
                    form.on('submit(erpPurchaseOrder-form-submit)', function (d) {

                        if(purchaseOrderDetails.length > 0){
                            var field = d.field;
                            field.purchaseOrderDetails = purchaseOrderDetails;
                            field.inquiryNo = data.inquiryNo;
                            field.quotationDetailIds = quotationDetailIds;
                            layer.load(2);
                            admin.req('enterprise/purchase/admin/purchaseOrder/add', JSON.stringify(field), function (data) {
                                layer.closeAll('loading');
                                if (data.code == 200) {
                                    layer.msg(data.msg, {icon: 1, time: 500});
                                    table.reload('erpPurchaseQuotationEnquiry-table', {});
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(data.msg, {icon: 2, time: 1000});
                                }
                            }, 'POST', false, "application/json");
                        }else{
                            layer.msg("未输入发生数据，无法保存", {icon: 2, time: 1000});
                        }
                        return false;
                    });

                }
            })
        };

        // 报价单
        var quotation = function (data) {
            var supplierIds = data.supplierIds;
            var purchaseRequisitionsId = data.purchaseRequisitionsId;
            var purchaseEnquiryId = data.id;
            var hasQuotationComplete = data.hasQuotationComplete;
            admin.open({
                type: 1,
                area: ['80%', '80%'],
                title: '询价单报价清单',
                content: $("#erpPurchaseQuotationEnquiry-quotationList").html(),
                success: function () {
                    if(hasQuotationComplete){
                        $("#erpPurchaseQuotationEnquiryBtnAdd").hide();
                    }

                    // 获取供应商
                    admin.req('enterprise/purchase/admin/supplierInfoPass/list', {limit: 99999}, function (data) {
                        if (200 == data.code) {
                            laytpl(erpPurchaseQuotationSupplierId_list.innerHTML).render(data.data, function (html) {
                                $("select[name='supplierId']").html(html);
                                form.render('select');
                            });
                        } else {
                            layer.msg('获取供应商失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    // 渲染表格
                    var quotationInsTb = table.render({
                        elem: '#erpPurchaseQuotation-table',
                        url: config.base + 'enterprise/purchase/admin/purchaseQuotation/list',
                        method: 'GET',
                        where: {
                            purchaseEnquiryId: purchaseEnquiryId
                        },
                        page: true,
                        cols: [[
                            {type: 'numbers'},
                            {field: 'quotationNo', align: 'center', title: '报价单号', width: 150},
                            {field: 'inquiryNo', align: 'center', title: '所属询价单号', width: 150},
                            {field: 'receiptsNo', align: 'center', title: '所属请购单号', width: 150},
                            {field: 'corporateName', align: 'center', title: '供应商', width: 120},
                            {field: 'quotedProductNumber', align: 'center', title: '报价物料数', width: 120},
                            {field: 'productNames', align: 'center', title: '物料名称', width: 120},
                            {field: 'assignNumber', align: 'center', title: '已分配物料数', width: 120},
                            {field: 'statusDesc', align: 'center', title: '状态', width: 100},
                            {field: 'createTime', align: 'center', title: '创建时间', width: 170},
                            {align: 'center', toolbar: '#erpPurchaseQuotation-table-bar', title: '操作',width: 200}
                        ]]
                    });

                    // 搜索
                    form.on('submit(quotationFormSubSearchLog)', function (data) {
                        quotationInsTb.reload({where: data.field,page: {curr: 1}},'data');
                    });

                    // 工具条点击事件
                    table.on('tool(erpPurchaseQuotation-table)', function (obj) {
                        var data = obj.data;
                        var layEvent = obj.event;
                        if(obj.event === 'confirm'){  //确认
                            confirm(obj);
                        } else if(obj.event === 'reject'){  //驳回
                            reject(obj);
                        } else if (obj.event === 'view') { //详情
                            showViewModel(data);
                        } else if (obj.event === 'cancel') { //取消
                            cancel(obj);
                        }
                    });

                    $('#erpPurchaseQuotationEnquiryBtnAdd').click(function () {
                        showEditModel();
                    });

                    //显示表单弹窗
                    var showEditModel = function (data) {
                        var addQuotation = admin.open({
                            type: 1,
                            title: data ? '更新报价单' : '添加报价单',
                            area: ['78%', '78%'],
                            content: $('#quotationForm').html(),
                            success: function () {

                                //自定义验证规则
                                form.verify({
                                    quantity: [/^([1-9]\d*(\.\d{1,2})?|([0](\.([0][0-9][1-9]|[1-9]\d{0,1})))|[ \t]*)$/, '格式错误'],
                                    rate: [/^([1-9]\d*(\.\d{1,2})?|([0](\.([0][1-9]|[1-9]\d{0,1})))|[ \t]*)$/, '格式错误']
                                })

                                // 渲染表格
                                var addQuotationTb = table.render({
                                    elem: '#quotation-add-table',
                                    url: config.base + 'enterprise/purchase/admin/purchaseEnquiryDetail/list',
                                    method: 'GET',
                                    where: {
                                        purchaseEnquiryId: purchaseEnquiryId
                                    },
                                    page: false,
                                    cols: [[
                                        {type: 'numbers'},
                                        {field: 'id', align: 'center', title: '询价单明细ID', hide: true},
                                         {field: 'skuCode', align: 'center', title: '物料编号'},
                                        {field: 'productName', align: 'center', title: '物料名称'},
                                        {field: 'specifications', align: 'center', title: '规格型号'},
                                        {field: 'unitName', align: 'center', title: '计量单位'},
                                        {field: 'quantity', align: 'center', title: '需求数量'},
                                        {field: 'offerTaxPrice', align: 'center', title: '报价含税单价', templet: '#offerTaxPrice'},
                                        {field: 'rate', align: 'center', title: '税率(%)', templet: '#rate'},
                                        {field: 'supply', align: 'center', title: '可供应量', templet: '#supply'}
                                    ]]
                                });

                                // 获取供应商
                                admin.req('enterprise/purchase/admin/supplierInfoPass/list', {limit: 99999, supplierIds: supplierIds
                                    , filterHasQuotationSupplier: 1, enquiryId: purchaseEnquiryId}, function (data) {
                                    if (200 == data.code) {
                                        console.log(data.data);
                                        laytpl(erpPurchaseOrderSupplierId.innerHTML).render(data.data, function (html) {
                                            $("form select[name='supplierId']").html(html);
                                            form.render('select');
                                            form.on('select(getSupplier)', function (res) {
                                                if(res.value != ""){
                                                    addQuotationTb.reload({where: {supplierId: res.value, filterQuotationDetail: 1}});
                                                    // $("#quotationList").show();
                                                }else{
                                                    addQuotationTb.reload({where: {supplierId: "", filterQuotationDetail: ""}});
                                                    // $("#quotationList").hide();
                                                }
                                            })
                                        });
                                    } else {
                                        layer.msg('获取供应商失败', {icon: 2, time: 1000});
                                    }
                                }, 'GET');

                                // 表单提交事件
                                form.on('submit(quotationFormSubmit)', function (d) {

                                    var purchaseQuotationDetails = [];
                                    // 获取明细数据
                                    var msg = "";
                                    $("#quotationList").find("tbody tr").each(function (index, item) {
                                        var purchaseEnquiryDetailId = ""; // 询价单明细ID
                                        $(item).find("td").each(function (tdIndex, tdItem) {
                                            if($(tdItem).data("field") == "id"){
                                                purchaseEnquiryDetailId = $(tdItem).find("div").text();
                                                return;
                                            }
                                        })
                                        var offerTaxPrice = $(item).find("input[name='offerTaxPrice']").val(); // 报价含税单价
                                        var rate = $(item).find("input[name='rate']").val(); // 税率
                                        var supply = $(item).find("input[name='supply']").val(); // 可供应量
                                        if(offerTaxPrice != "" || rate != "" || supply != ""){
                                            if(offerTaxPrice == "" || rate == "" || supply == ""){
                                                msg = "请将第"+(index+1)+"行数据完善";
                                                return;
                                            }else{
                                                var detail = {};
                                                detail.purchaseEnquiryDetailId = purchaseEnquiryDetailId;
                                                detail.offerTaxPrice = offerTaxPrice;
                                                detail.rate = rate;
                                                detail.supply = supply;
                                                purchaseQuotationDetails.push(detail)
                                                return;
                                            }
                                        }
                                    })
                                    if(msg != ""){
                                        layer.msg(msg, {icon: 2, time: 1000});
                                        return false;
                                    }

                                    if(purchaseQuotationDetails.length > 0){
                                        d.field.purchaseEnquiryId = purchaseEnquiryId;
                                        d.field.purchaseQuotationDetails = purchaseQuotationDetails;
                                        layer.load(2);
                                        admin.req(data ? 'enterprise/purchase/admin/purchaseQuotation/update' : 'enterprise/purchase/admin/purchaseQuotation/add', JSON.stringify(d.field), function (res) {
                                            layer.closeAll('loading');
                                            if (res.code == 200) {
                                                layer.msg(res.msg, {icon: 1});
                                                table.reload('erpPurchaseQuotation-table');
                                                layer.close(addQuotation);
                                            } else {
                                                layer.msg(res.msg, {icon: 2});
                                            }
                                        }, data ? 'PUT' : 'POST', false, "application/json");
                                    }else{
                                        layer.msg("未输入发生数据，无法保存", {icon: 2, time: 1000});
                                    }
                                    return false;
                                });

                            }
                        })
                    };

                    // 确认
                    var confirm = function (obj) {
                        layer.confirm('要确认吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('enterprise/purchase/admin/purchaseQuotation/confirm', {id:obj.data.id}, function (data) {
                                layer.closeAll('loading');
                                if (data.code == 200) {
                                    layer.msg(data.msg, {icon: 1, time: 500});
                                    table.reload('erpPurchaseQuotation-table');
                                } else {
                                    layer.msg(data.msg, {icon: 2, time: 1000});
                                }
                            }, 'GET');
                        });
                    };

                    // 驳回
                    var reject = function (obj) {
                        layer.confirm('要驳回吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('enterprise/purchase/admin/purchaseQuotation/reject', {id:obj.data.id}, function (data) {
                                layer.closeAll('loading');
                                if (data.code == 200) {
                                    layer.msg(data.msg, {icon: 1, time: 500});
                                    table.reload('erpPurchaseQuotation-table');
                                } else {
                                    layer.msg(data.msg, {icon: 2, time: 1000});
                                }
                            }, 'GET');
                        });
                    };

                    // 取消确认
                    var cancel = function (obj) {
                        layer.confirm('要取消确认吗？',{
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('enterprise/purchase/admin/purchaseQuotation/cancel', {id:obj.data.id}, function (data) {
                                layer.closeAll('loading');
                                if (data.code == 200) {
                                    layer.msg(data.msg, {icon: 1, time: 500});
                                    table.reload('erpPurchaseQuotation-table');
                                } else {
                                    layer.msg(data.msg, {icon: 2, time: 1000});
                                }
                            }, 'GET');
                        });
                    };

                    //详情
                    var showViewModel = function (data) {
                        var quotationDetail = admin.open({
                            type: 1,
                            title: '报价单详情',
                            area: ['80%', '75%'],
                            content: $('#quotation-detail').html(),
                            success: function () {
                                // 渲染表格
                                table.render({
                                    elem: '#quotation-detail-table',
                                    url: config.base + 'enterprise/purchase/admin/purchaseQuotationDetail/list',
                                    method: 'GET',
                                    cellMinWidth: 100,
                                    where: {
                                        purchaseQuotationId: data.id,
                                        purchaseRequisitionsId: purchaseRequisitionsId,
                                    },
                                    page: true,
                                    cols: [[
                                        {type: 'numbers'},
                                        {field: 'skuCode', align: 'center', title: '物料编号'},
                                        {field: 'productName', align: 'center', title: '物料名称'},
                                        {field: 'specifications', align: 'center', title: '规格'},
                                        {field: 'unitName', align: 'center', title: '单位'},
                                        // {field: 'inventoryClassName', align: 'center', title: '所属分类', width: 120},
                                        {field: 'offerTaxPrice', align: 'center', title: '报价含税单价'},
                                        {field: 'offerPrice', align: 'center', title: '报价除税单价'},
                                        {field: 'rate', align: 'center', title: '税率(%)'},
                                        {field: 'demandQuantity', align: 'center', title: '需求数量'},
                                        {field: 'supply', align: 'center', title: '可供应量'},
                                        {field: 'allocationPurchases', align: 'center', title: '分配采购量', templet: function (d) {
                                                return d.hasAssign ? d.allocationPurchases : "未分配"
                                            }},
                                        {field: 'createTime', align: 'center', title: '创建时间', width: 170},
                                        {align: 'center', toolbar: '#quotation-detail-table-bar', title: '操作',width: 180, fixed: 'right'}
                                    ]]
                                });

                                // 工具条点击事件
                                table.on('tool(quotation-detail-table)', function (obj) {
                                    var data = obj.data;
                                    var layEvent = obj.event;
                                    if(obj.event === 'assign'){  //分配采购量
                                        assign(data.id, data);
                                    } else if(obj.event === 'contrast'){  //对比
                                        contrast(data);
                                    }
                                });

                                // 分配采购量
                                var assign = function (quotationDetailId, quotationDetail) {
                                    admin.open({
                                        id:1,
                                        type: 1,
                                        title:'分配采购量',
                                        // skin:'layui-layer-rim',
                                        area:['450px', 'auto'],
                                        content: $('#assign-model').html(),
                                        success: function () {
                                            form.val('assign-form', quotationDetail);
                                        },
                                        btn:['保存','取消'],
                                        btn1: function (index,layero) {
                                            var allocationPurchases = $("input[name='allocationPurchases']").val();
                                            if(allocationPurchases != ""){
                                                if(allocationPurchases > 0){
                                                    admin.req('enterprise/purchase/admin/purchaseQuotationDetail/allocationPurchases', {id: quotationDetailId, allocationPurchases: allocationPurchases}, function (data) {
                                                        layer.closeAll('loading');
                                                        if (data.code == 200) {
                                                            layer.msg(data.msg, {icon: 1, time: 500});
                                                            table.reload('quotation-detail-table');
                                                            table.reload('contrastQuotation-detail-table');
                                                            table.reload('viewModelDetail-table');
                                                            layer.close(index);
                                                        } else {
                                                            layer.msg(data.msg, {icon: 2, time: 1000});
                                                        }
                                                    }, 'GET');
                                                }else{
                                                    layer.msg("分配采购量必须大于0", {icon: 2, time: 1000});
                                                }
                                            }else{
                                                layer.msg("请输入分配采购量", {icon: 2, time: 1000});
                                            }
                                        },
                                        btn2:function (index,layero) {
                                            layer.close(index);
                                        }
                                    });
                                };

                                // 对比
                                var contrast = function (data) {
                                    var contrastQuotationDetail = admin.open({
                                        type: 1,
                                        title: '对比供应商报价详情',
                                        area: ['84%', '80%'],
                                        content: $('#contrastQuotation-detail').html(),
                                        success: function () {
                                            // 渲染表格
                                            table.render({
                                                elem: '#contrastQuotation-detail-table',
                                                url: config.base + 'enterprise/purchase/admin/purchaseQuotationDetail/list',
                                                cellMinWidth: 100,
                                                method: 'GET',
                                                where: {
                                                    purchaseRequisitionsId: purchaseRequisitionsId,
                                                    skuCode: data.skuCode
                                                },
                                                page: true,
                                                cols: [[
                                                    {type: 'numbers'},
                                                    {field: 'corporateName', align: 'center', title: '供应商'},
                                                    {field: 'skuCode', align: 'center', title: '物料编号'},
                                                    {field: 'productName', align: 'center', title: '物料名称'},
                                                    {field: 'specifications', align: 'center', title: '规格'},
                                                    {field: 'unitName', align: 'center', title: '单位'},
                                                    // {field: 'inventoryClassName', align: 'center', title: '所属分类'},
                                                    {field: 'offerTaxPrice', align: 'center', title: '报价含税单价'},
                                                    {field: 'offerPrice', align: 'center', title: '报价除税单价'},
                                                    {field: 'rate', align: 'center', title: '税率(%)'},
                                                    {field: 'demandQuantity', align: 'center', title: '需求数量'},
                                                    {field: 'supply', align: 'center', title: '可供应量'},
                                                    {field: 'allocationPurchases', align: 'center', title: '分配采购量', templet: function (d) {
                                                            return d.hasAssign ? d.allocationPurchases : "未分配"
                                                        }},
                                                    {field: 'createTime', align: 'center', title: '创建时间'},
                                                    {align: 'center', toolbar: '#contrastQuotation-detail-table-bar', title: '操作',width: 180, fixed: 'right'}
                                                ]]
                                            });

                                            // 工具条点击事件
                                            table.on('tool(contrastQuotation-detail-table)', function (obj) {
                                                var data = obj.data;
                                                var layEvent = obj.event;
                                                if(obj.event === 'assign'){  //分配采购量
                                                    assign(data.id, data);
                                                } else if(obj.event === 'view'){  //详情
                                                    viewModel(data);
                                                }
                                            });

                                            var viewModel = function (data) {
                                                var viewModelDetail = admin.open({
                                                    type: 1,
                                                    title: '供应商可报价物料详情',
                                                    area: ['80%', '75%'],
                                                    content: $('#viewModelDetail').html(),
                                                    success: function () {
                                                        // 渲染表格
                                                        table.render({
                                                            elem: '#viewModelDetail-table',
                                                            url: config.base + 'enterprise/purchase/admin/purchaseEnquiryDetail/list',
                                                            method: 'GET',
                                                            cellMinWidth: 100,
                                                            where: {
                                                                purchaseRequisitionsId: purchaseRequisitionsId,
                                                                supplierId: data.supplierId
                                                            },
                                                            page: true,
                                                            cols: [[
                                                                {type: 'numbers'},
                                                                {field: 'skuCode', align: 'center', title: '物料编号'},
                                                                {field: 'productName', align: 'center', title: '物料名称'},
                                                                {field: 'specifications', align: 'center', title: '规格'},
                                                                {field: 'unitName', align: 'center', title: '单位'},
                                                                // {field: 'inventoryClassName', align: 'center', title: '所属分类'},
                                                                {field: 'offerTaxPrice', align: 'center', title: '报价含税单价', width: 120, templet: function (d) {
                                                                        return d.hasQuotation ? d.offerTaxPrice : "-"
                                                                    }},
                                                                {field: 'offerPrice', align: 'center', title: '报价除税单价', width: 120, templet: function (d) {
                                                                        return d.hasQuotation ? d.offerPrice : "-"
                                                                    }},
                                                                {field: 'rate', align: 'center', title: '税率(%)', templet: function (d) {
                                                                        return d.hasQuotation ? d.rate : "-"
                                                                    }},
                                                                {field: 'demandQuantity', align: 'center', title: '需求数量', templet: function (d) {
                                                                        return d.hasQuotation ? d.demandQuantity : "-"
                                                                    }},
                                                                {field: 'supply', align: 'center', title: '可供应量', templet: function (d) {
                                                                        return d.hasQuotation ? d.supply : "-"
                                                                    }},
                                                                {field: 'allocationPurchases', align: 'center', title: '分配采购量', width: 120, templet: function (d) {
                                                                        return d.hasQuotation ? d.hasAssign ? d.allocationPurchases : "未分配" : "-"
                                                                    }},
                                                                {field: 'createTime', align: 'center', title: '创建时间', width: 170},
                                                                {align: 'center', toolbar: '#viewModelDetail-table-bar', title: '操作', width: 160}
                                                            ]]
                                                        });

                                                        // 工具条点击事件
                                                        table.on('tool(viewModelDetail-table)', function (obj) {
                                                            var data = obj.data;
                                                            var layEvent = obj.event;
                                                            if(obj.event === 'assign'){  //分配采购量
                                                                assign(data.quotationDetailId, data);
                                                            } else if(obj.event === 'contrast'){  //对比
                                                                table.reload('contrastQuotation-detail-table', {page: {curr:1}, where: {
                                                                        purchaseRequisitionsId: purchaseRequisitionsId, skuCode: data.skuCode}});
                                                                layer.close(viewModelDetail);
                                                            }
                                                        });

                                                    }
                                                })
                                            }
                                        }
                                    })
                                };
                            }
                        })
                    };

                }
            })
        };

        //详情
        var showViewModel = function (data) {
            var purchaseRequisitionsId = data.purchaseRequisitionsId;
            admin.open({
                type: 1,
                area: ['78%', '78%'],
                title: '询价单报价清单',
                content: $("#erpPurchaseQuotationEnquiry-view").html(),
                success: function () {
                    form.val('erpPurchaseQuotationEnquiry-view', data);

                    // 询价单报价清单
                    var purchaseEnquiryDetailList = function() {
                        //获取明细数据
                        admin.req('enterprise/purchase/admin/purchaseEnquiryDetail/list',
                            {purchaseRequisitionsId: purchaseRequisitionsId, purchaseEnquiryId: data.id}, function (data) {
                            layer.closeAll('loading');
                            if (200 == data.code) {
                                var detail = data.data;
                                // 渲染表格
                                table.render({
                                    elem: '#erpPurchaseQuotation-add-table',
                                    data: detail,
                                    limit: detail.length,
                                    height: 430,
                                    cols: [[
                                        {type: 'numbers'},
                                        {field: 'skuCode', align:'center', title: '物料编号', width: '120'},
                                        {field: 'productName',align:'center', title: '物料名称', width: '150'},
                                        {field: 'specifications',  align:'center', title: '规格型号', width: '120'},
                                        {field: 'unitName', align:'center', title: '采购单位', width: '100'},
                                        // {field: 'inventoryClassName', align: 'center', title: '所属分类', width: '150'},
                                        {field: 'demandQuantity', align: 'center', title: '需求数量', width: '100'},
                                        {field: 'totalAllocationPurchases', align:'center', title: '分配量', width: '100'},
                                        {align: 'center', title: '已报价供应商数/询价供应商数',width: 220, templet: function (d) {
                                                if(d.hasPublic){
                                                    return d.quotedSupplierNumber
                                                }else{
                                                    return d.quotedSupplierNumber+"<span style='padding: 0 10px;'>/</span>"+d.inquirySupplierNumber;
                                                }
                                            }},
                                        {field: 'quotationsNumber', align: 'center', title: '报价单数',width: 100},
                                        {field: 'createTime', align:'center', title: '创建时间', width: '170'},
                                        {align: 'center', toolbar: '#erpPurchaseQuotation-add-table-bar', title: '操作',width: 80}
                                    ]]
                                });
                            } else {
                                layer.msg('获取明细失败', {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    }

                    purchaseEnquiryDetailList();

                    // 工具条点击事件
                    table.on('tool(erpPurchaseQuotation-add-table)', function (obj) {
                        var data = obj.data;
                        var layEvent = obj.event;
                        if (obj.event === 'view') { //详情
                            contrast(data);
                        }
                    });

                    // 分配采购量
                    var assign = function (quotationDetailId, quotationDetail) {
                        admin.open({
                            id:1,
                            type: 1,
                            title:'分配采购量',
                            // skin:'layui-layer-rim',
                            area:['450px', 'auto'],
                            content: $('#assign-model').html(),
                            success: function () {
                                form.val('assign-form', quotationDetail);
                            },
                            btn:['保存','取消'],
                            btn1: function (index,layero) {
                                var allocationPurchases = $("input[name='allocationPurchases']").val(); // 分配采购量
                                if(allocationPurchases != ""){
                                    if(allocationPurchases > 0){
                                        admin.req('enterprise/purchase/admin/purchaseQuotationDetail/allocationPurchases', {id: quotationDetailId, allocationPurchases: allocationPurchases}, function (data) {
                                            layer.closeAll('loading');
                                            if (data.code == 200) {
                                                layer.msg(data.msg, {icon: 1, time: 500});
                                                table.reload('quotation-detail-table');
                                                table.reload('contrastQuotation-detail-table');
                                                table.reload('viewModelDetail-table');
                                                purchaseEnquiryDetailList();
                                                layer.close(index);
                                            } else {
                                                layer.msg(data.msg, {icon: 2, time: 1000});
                                            }
                                        }, 'GET');
                                    }else{
                                        layer.msg("分配采购量必须大于0", {icon: 5, time: 1000});
                                    }
                                }else{
                                    layer.msg("请输入分配采购量", {icon: 5, time: 1000});
                                }
                            },
                            btn2:function (index,layero) {
                                layer.close(index);
                            }
                        });
                    };

                    // 对比
                    var contrast = function (data) {
                        var contrastQuotationDetail = admin.open({
                            type: 1,
                            title: '对比供应商报价详情',
                            area: ['84%', '80%'],
                            content: $('#contrastQuotation-detail').html(),
                            success: function () {
                                // 渲染表格
                                table.render({
                                    elem: '#contrastQuotation-detail-table',
                                    url: config.base + 'enterprise/purchase/admin/purchaseQuotationDetail/list',
                                    method: 'GET',
                                    cellMinWidth: 100,
                                    where: {
                                        purchaseRequisitionsId: purchaseRequisitionsId,
                                        skuCode: data.skuCode
                                    },
                                    page: true,
                                    cols: [[
                                        {type: 'numbers'},
                                        {field: 'corporateName', align: 'center', title: '供应商'},
                                        {field: 'skuCode', align: 'center', title: '物料编号'},
                                        {field: 'productName', align: 'center', title: '物料名称'},
                                        {field: 'specifications', align: 'center', title: '规格'},
                                        {field: 'unitName', align: 'center', title: '单位'},
                                        // {field: 'inventoryClassName', align: 'center', title: '所属分类'},
                                        {field: 'offerTaxPrice', align: 'center', title: '报价含税单价'},
                                        {field: 'offerPrice', align: 'center', title: '报价除税单价'},
                                        {field: 'rate', align: 'center', title: '税率(%)'},
                                        {field: 'demandQuantity', align: 'center', title: '需求数量'},
                                        {field: 'supply', align: 'center', title: '可供应量'},
                                        {field: 'allocationPurchases', align: 'center', title: '分配采购量', templet: function (d) {
                                                return d.hasAssign ? d.allocationPurchases : "未分配"
                                            }},
                                        {field: 'createTime', align: 'center', title: '创建时间'},
                                        {align: 'center', toolbar: '#contrastQuotation-detail-table-bar', title: '操作',width: 180, fixed: 'right'}
                                    ]]
                                });

                                // 工具条点击事件
                                table.on('tool(contrastQuotation-detail-table)', function (obj) {
                                    var data = obj.data;
                                    var layEvent = obj.event;
                                    if(obj.event === 'assign'){  //分配采购量
                                        assign(data.id, data);
                                    } else if(obj.event === 'view'){  //详情
                                        viewModel(data);
                                    }
                                });

                                var viewModel = function (data) {
                                    var viewModelDetail = layer.open({
                                        type: 1,
                                        title: '供应商可报价物料详情',
                                        area: ['80%', '600px'],
                                        offset: ['200px', '280px'],
                                        content: $('#viewModelDetail').html(),
                                        success: function () {
                                            // 渲染表格
                                            table.render({
                                                elem: '#viewModelDetail-table',
                                                url: config.base + 'enterprise/purchase/admin/purchaseEnquiryDetail/list',
                                                method: 'GET',
                                                cellMinWidth: 100,
                                                where: {
                                                    purchaseRequisitionsId: purchaseRequisitionsId,
                                                    supplierId: data.supplierId
                                                },
                                                page: true,
                                                cols: [[
                                                    {type: 'numbers'},
                                                    {field: 'skuCode', align: 'center', title: '物料编号'},
                                                    {field: 'productName', align: 'center', title: '物料名称'},
                                                    {field: 'specifications', align: 'center', title: '规格'},
                                                    {field: 'unitName', align: 'center', title: '单位', width: 100},
                                                    // {field: 'inventoryClassName', align: 'center', title: '所属分类'},
                                                    {field: 'offerTaxPrice', align: 'center', title: '报价含税单价', templet: function (d) {
                                                            return d.hasQuotation ? d.offerTaxPrice : "-"
                                                        }},
                                                    {field: 'offerPrice', align: 'center', title: '报价除税单价', templet: function (d) {
                                                            return d.hasQuotation ? d.offerPrice : "-"
                                                        }},
                                                    {field: 'rate', align: 'center', title: '税率(%)', width: 100, templet: function (d) {
                                                            return d.hasQuotation ? d.rate : "-"
                                                        }},
                                                    {field: 'demandQuantity', align: 'center', title: '需求数量', width: 100, templet: function (d) {
                                                            return d.hasQuotation ? d.demandQuantity : "-"
                                                        }},
                                                    {field: 'supply', align: 'center', title: '可供应量', width: 100, templet: function (d) {
                                                            return d.hasQuotation ? d.supply : "-"
                                                        }},
                                                    {field: 'allocationPurchases', align: 'center', title: '分配采购量', width: 120, templet: function (d) {
                                                            return d.hasQuotation ? d.hasAssign ? d.allocationPurchases : "未分配" : "-"
                                                        }},
                                                    {field: 'createTime', align: 'center', title: '创建时间', width: 170},
                                                    {align: 'center', toolbar: '#viewModelDetail-table-bar', title: '操作',width: 180}
                                                ]]
                                            });

                                            // 工具条点击事件
                                            table.on('tool(viewModelDetail-table)', function (obj) {
                                                var data = obj.data;
                                                var layEvent = obj.event;
                                                if(obj.event === 'assign'){  //分配采购量
                                                    assign(data.quotationDetailId, data);
                                                } else if(obj.event === 'contrast'){  //对比
                                                    table.reload('contrastQuotation-detail-table', {page: {curr:1}, where: {
                                                            purchaseRequisitionsId: purchaseRequisitionsId, skuCode: data.skuCode}});
                                                    layer.close(viewModelDetail);
                                                }
                                            });

                                        }
                                    })
                                }
                            }
                        })
                    };

                }
            })
        };

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 导出excel
        $('#erpPurchaseQuotationEnquiryBtnExport').click(function () {
            var checkRows = table.checkStatus('erpPurchaseQuotationEnquiry-table');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>